package cn.gson.crm.controller;

import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.model.dao.EmpDao;
import cn.gson.crm.model.entity.Employee;

/**
 * 登录
 */
@WebServlet("/login")
public class LoginServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private EmpDao empDao = new EmpDao();
	private String loginJsp = "login.jsp";

	@Override
	protected void get(HttpServletRequest request, HttpServletResponse resp) throws Exception {
		forward(loginJsp);
		
	}

	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 获取参数

				String empName = request.getParameter("empName");
				String empPassword = request.getParameter("empPassword");
				empPassword = addPass(empPassword);
				// 验证
				if (empName == null || empName.isEmpty()) {
					errorForward("姓名不能为空！", loginJsp);
					return;
				}
				if (empPassword == null || empPassword.isEmpty()) {
					errorForward("密码不能为空！", loginJsp);
					return;
				}
				// 通过用户名查找对应的用户
				Employee e = empDao.getEmpByEmpName(empName);
				
				// 判断是否找到这个用户
				if (e != null && e.getEmpPassword().equals(empPassword)) {
					// 用户存在，并且密码匹配
					request.getSession().setAttribute("emp", e);	
					
					redirect("uimain");
				} else {
					errorForward("姓名或密码错误！", loginJsp);
				}
	}
}
